import os
import optparse

import pyglet
### disable error checking for increased performance
##pyglet.options['debug_gl'] = False
from pyglet import gl, font

import gg

#import modules that need to register services 
import splash_scene

import cocos
from cocos.director import director

def main():
    # make available options
    parser = optparse.OptionParser()
    parser.add_option("-d", "--devmode",
                      action="store_true", dest="devmode", default=False,
                      help="developer mode on")
    parser.add_option("-x", "--width", type='int', dest="width", default='800',
                      help="set window width", metavar="WIDTH")
    parser.add_option("-y", "--height", type="int", dest="height", default='600',
                      help="set window height", metavar="HEIGHT")
    parser.add_option("-F", "--fullscreen", dest="fullscreen", default=False,
                      help="set fullscreen mode on", action="store_true")
    parser.add_option("-s", "--stage", type="int", dest="first_stage",default=0,
                      help="first stage to play")

    (options, args) = parser.parse_args()

    #copy to globals module gg each value you need available.
    gg.devmode = options.devmode
    gg.first_stage = options.first_stage

    #detect if we have audio
    try:
        import pyglet.media.avbin
        gg.sound_available = True
    except ImportError:
        gg.sound_available = False
        print 'Not detected AVbin: Audio will not be available'
        print 'You can download AVbin from http://code.google.com/p/avbin/'


    # fix pyglet resource path
##    data_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
    # calc paths
    data_path = 'data'
    pyglet.resource.path.append(data_path)
    pyglet.resource.reindex()
    gg.data_dir = os.path.abspath(data_path)
##    gg.levels_dir = os.path.join(data_path,'levels')
##    gg.actors_dir = os.path.join(data_path,'actors')

    # fonts stuff
    fonts_path = os.path.join(data_path, 'fonts')
    font.add_directory(fonts_path)

    # initialize cocos director
    if options.fullscreen:
        director.init(fullscreen=options.fullscreen)
    else:
        director.init(width=int(options.width), height=int(options.height), resizable=True)

    #<----- startup end - all subsystem initialized except audio -
    #       services availables - audio will be initialized in splash screen

    #scene = gg.services["s_get_splash_scene"]()
    #import level_scene ; scene = gg.services["s_get_game_scene"]()
    import ui_scene ; scene = gg.services["s_get_ui_scene"]()
    #scene = gg.services["s_get_hiscores_scene"]()
    
    director.run(scene)

    #sound.init()

    
